package com.example.administrator.maizuofilm.cinema.adapter;

import java.util.List;

import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.Cinema;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandAdapter extends BaseExpandableListAdapter {

	private  List<String> parentList;
	private  List<List<Cinema>> childList;
	private  LayoutInflater  layout;
	
	public  MyExpandAdapter(Context context,List<String> parent,List<List<Cinema>> child){
		this.parentList=parent;
		this.childList=child;
		layout=LayoutInflater.from(context);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}
	@Override
	public int getChildrenCount(int childPosition) {
		// TODO Auto-generated method stub
		return childList.get(childPosition).size();
	}

	


	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return parentList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return parentList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}



	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		   ViewHolder holder=null;
		   if(convertView==null){
			   convertView=layout.inflate(R.layout.cinema_child_item, null);
			   holder=new ViewHolder();
			   ViewUtils.inject(holder, convertView);
			   convertView.setTag(holder);
		   }else{
			   holder=(ViewHolder) convertView.getTag();
		   }
		   Cinema  cm=childList.get(groupPosition).get(childPosition);
		   holder.cName.setText(cm.getCinemaName());
		   holder.cAddress.setText(cm.getAddress());
		   holder.cDistance.setText("5km");
		   holder.cCount.setText(cm.getMovieCount()+"部电影正在上映");
		return convertView;
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		TextView  parentTextV=null;
		if(convertView==null){
			 convertView=layout.inflate(R.layout.cinema_parent_item, null);
			 parentTextV=(TextView) convertView.findViewById(R.id.cinema_parent_text);
			 convertView.setTag(parentTextV);
		}else{
			parentTextV=(TextView) convertView.getTag();
		}
		 parentTextV.setText(parentList.get(groupPosition));   
		return convertView;
	}
    class  ViewHolder{
    	@ViewInject(R.id.cinema_child_name)
    	 TextView  cName;
    	@ViewInject(R.id.cinema_child_address)
    	 TextView  cAddress;
    	@ViewInject(R.id.cinema_child_distance)
    	 TextView  cDistance;
    	@ViewInject(R.id.cinema_child_count)
    	 TextView  cCount;
    }
}
